package com.study.bigdata.spark.sql

import org.apache.spark.SparkConf
import org.apache.spark.rdd.RDD
import org.apache.spark.sql.{DataFrame, Dataset, Row, SparkSession}

object Spark02_SparkSQL_UDF {
  def main(args: Array[String]): Unit = {
    // TODO 创建SparkSql的运行环境   环境+对象+隐式转换
    val sparkConf = new SparkConf().setMaster("local[*]").setAppName("sparkSQL")
    val spark = SparkSession.builder().config(sparkConf).getOrCreate()
    import spark.implicits._
    // TODO 执行逻辑
    val df: DataFrame = spark.read.json("data/user.json")
    df.createOrReplaceTempView("user")
    spark.udf.register("prefixName",(name:String)=>{
      "Name: "+name
    })
    spark.sql("select age,prefixName(username) from user").show()
    /*
+---+--------------------+
|age|prefixName(username)|
+---+--------------------+
| 20|      Name: zhangsan|
| 30|          Name: lisi|
| 40|        Name: wangwu|
+---+--------------------+
     */
    // TODO 关闭环境
    spark.close()
  }
}
